package cn.autumnorange.app.common.consumer.security;

import cn.autumnorange.app.common.rpc.exception.JsonException;
import cn.autumnorange.app.common.rpc.exception.JsonExceptionImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
// 集成redisSession下,测试得出授权服务器需要header传入SESSIONID,SESSION俩个值;
// DefaultSaveRequest()才能获取到本次会话客户端服务器的请求信息 对于客户端的JSESSIONID应该还要传入

/**
 * Created on 2018/4/28.
 *
 * @author wsf
 * @since 1.0
 */
// 解决 CROSSSLOT Keys in request don't hash to the same slot
// https://blog.csdn.net/zl_momomo/article/details/81911705

public abstract class AbstractResourceServerConfigurer extends ResourceServerConfigurerAdapter {
  private JsonException jsonExceptionImpl;

  private JsonOAuth2AuthenticationEntryPoint jsonOAuth2AuthenticationEntryPoint;

  public AbstractResourceServerConfigurer() {
    //    OAuth2AuthenticationEntryPoint
    this.jsonExceptionImpl = new JsonExceptionImpl();
    this.jsonOAuth2AuthenticationEntryPoint =
            new JsonOAuth2AuthenticationEntryPoint(jsonExceptionImpl);
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {

    http.exceptionHandling()
        .authenticationEntryPoint(jsonOAuth2AuthenticationEntryPoint)
        .and()
        //        启用跨域
        .cors()
        // 禁用跨域检验
        //        .disable()
        .and()
        // 禁用跨站伪造
        .csrf()
        .disable()
        .authorizeRequests()
        .anyRequest() // 允许所有请求匿名访问
        .permitAll();
  }

  public static void main(String[] args) throws Exception {
//        System.out.println(  tryTest1(8));

//        System.out.println(  tryTest2(8));
//        System.out.println(tryTest3(8));
//    System.out.println(tryTest4(8));
//    System.out.println("return "+tryTest5(8));
    System.out.println("return "+tryTest6(9));
//      System.out.println("报错还能跑");

//     tryTest7(8);
//    System.out.println("return "+tryTest8(8));



  }


  public static int tryTest1(int i) {
    try {
      System.out.println("try里面的语句执行了！");
      return i;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.out.println("final执行了");
      i = i + 2;
      return i;
    }
  }

  public static int tryTest2(int i) {
    try {
      System.out.println("try里面的语句执行了！");
      int number = 8 / 0;
      return i;
    } catch (Exception e) {
      System.out.println("catch里面的语句执行了！");
      return i + 1;
    } finally {
      System.out.println("final执行了");
    }
  }

  public static int tryTest3(int i) {
    try {
      System.out.println("try里面的语句执行了！");
      int number = 8 / 0;
      return i;
    } catch (Exception e) {
      System.out.println("catch里面的语句执行了！");
      return i + 1;
    } finally {
      System.out.println("final执行了");
      return i + 2;
    }
  }

  public static int tryTest4(int i) {
    int result = 0;
    try {
      System.out.println("try里面的语句执行了！");
      return result;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.out.println("final执行了");
      result = i;
    }
    return -1;
  }

  public static Integer tryTest5(int i){
    Integer obj = i;
    try {
      System.out.println("try里面的语句执行了！");
      return obj;
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      System.out.println("final执行了");
      obj = null;
//      return obj;
    }
    return -1;
  }
  /*
  * 有finally catch抛出异常是无法抛出的
   */
    public static int tryTest6(int i) throws Exception {
      Exception e1 = null;
      if(i==8){
         throw new RuntimeException();

      }
        try {
            System.out.println("try里面的语句执行了！");
            int number = 8 / 0;
            return i;
        } catch (Exception e) {
            System.out.println("catch里面的语句执行了！");
          e1=e;
        } finally {
          System.out.println("final执行了");

          if(e1!=null){
            System.out.println("final抛出异常执行了"+e1.getMessage());
            throw e1;
          }
            return i + 1;
        }
    }

    public static void tryTest7(int i) throws Exception {
      try{
          System.out.println("return "+tryTest6(8));
          System.out.println("报错还能跑");
      }catch (Exception e){
          System.out.println(e);

      }finally{

      }

    }

  /**
   * 没有finally catch里面抛出异常  可以抛出异常
   * @param i
   * @return
   */
  public static int tryTest8(int i){
    Exception e1 = null;

    int number=0;

    try {
      System.out.println("try里面的语句执行了！");
      if(i==8)
       number = 8 / 0;
    } catch (Exception e) {
      System.out.println("catch里面的语句执行了！");
      throw e;
    }
    return number;

  }
    }
